package com.movitech.mobile.cz.base.supplier.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.NumberUtils;
import com.movitech.mobile.cz.base.modules.config.SupplierBigStatusEnum;
import com.movitech.mobile.cz.base.supplier.entity.SSupplierRcRelate;
import com.movitech.mobile.cz.base.supplier.mapper.SSupplierRcRelateMapper;
import com.movitech.mobile.cz.base.supplier.mapper.ext.ExtSSupplierRcRelateMapper;
import com.movitech.mobile.cz.base.supplier.service.SSupplierRcRelateService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 供方与地区公司关联 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@Service("sSupplierRcRelateService")
@Transactional
public class SSupplierRcRelateServiceImpl extends ServiceImpl<SSupplierRcRelateMapper, SSupplierRcRelate> implements SSupplierRcRelateService {

	@Autowired
	private ExtSSupplierRcRelateMapper extSSupplierRcRelateMapper;

	@Override
	public List<SSupplierRcRelate> getSupplierRcRelateBySuppliersId(String suppliersId) {
		return extSSupplierRcRelateMapper.getSupplierRcRelateBySuppliersId(suppliersId);
	}

	@Override
	public SSupplierRcRelate getSupplierRcRelateById(String supplierRcRelateId) {
		return extSSupplierRcRelateMapper.getExtId(supplierRcRelateId);
	}

	@Override
	public int addSupplierRcRelateByUserAndSupplierId(String userId, String supplierId) {
		return extSSupplierRcRelateMapper.addSupplierRcRelateByUserAndSupplierId(userId, supplierId);
	}

	@Override
	public void emptyActualIdsById(String id) {
		extSSupplierRcRelateMapper.emptyActualIdsById(id);
	}

	@Override
	public boolean hasQulifiedSSupplierRcRelate(String suppliersId){
		boolean isHas = false;
		List<SSupplierRcRelate> lstSSupplierRcRelate = getSupplierRcRelateBySuppliersId(suppliersId);
		if(CollectionUtils.isNotEmpty(lstSSupplierRcRelate)){
			for(SSupplierRcRelate item : lstSSupplierRcRelate){
				if(NumberUtils.toInteger(item.getSupplierBigStatus()+"") == SupplierBigStatusEnum.QUALIFIED.getCode()){
					isHas = true;
					break;
				}
			}
		}
		return isHas;
	}

	@Override
	public boolean hasFirstStoredSSupplierRcRelate(String suppliersId){
		boolean isHas = false;
		List<SSupplierRcRelate> lstSSupplierRcRelate = getSupplierRcRelateBySuppliersId(suppliersId);
		if(CollectionUtils.isNotEmpty(lstSSupplierRcRelate)){
			for(SSupplierRcRelate item : lstSSupplierRcRelate){
				if(NumberUtils.toInteger(item.getFirstStoredFlag()+"") == CommonConstants.YES_INTEGER
						&& NumberUtils.toInteger(item.getSupplierSource()+"") == CommonConstants.SUPPLIER_SOURCE_TYPE_INIT){
					isHas = true;
					break;
				}
			}
		}
		return isHas;
	}
}
